package com.hutra.utils {
    public class SingleList{
        private static var init:SingleList = null;
		private var list:Array = new Array();
		private var stage:* = null; // as DisplayObjectContainer, as Stage, as MovieClip that you want to sort
        public function SingleList(e:SingletonListSort) { 
			trace("new instance of singleton created");
			trace("Don't forget, in this special cases, you must add the stage!");
		}
        public static function getInstance():SingleList{
            if(init==null) init=new SingleList(new SingletonListSort());
            return init;
        }		
		///////////////////////////////////////////////
		//
		//
		//					START LIST!
		//
		//
		///////////////////////////////////////////////
        
		public function setList(value:Array):void { 
			list = value; 
		}
		public function setElement(value:*):void {
			list.push(value);
		}
		
		public function getList():Array { 
			return list; 
		}
		public function getListIndex(value:*):int {
			for (var i:int = 0; i < list.length; ) {
				if (list[i]==value) {
					return i;
				}
			}
			return -1;
		}
		public function getListIndexByName(name:*):int {
			for (var i:int = 0; i < list.length; ) {
				if (list[i].name==name) {
					return i;
				}
			}
			return -1;
		}
		public function getListIndexById(id:*):int {
			for (var i:int = 0; i < list.length; ) {
				if (list[i].id==id) {
					return i;
				}
			}
			return -1;
		}
		
		public function removeElement(value:*):void { 
			for (var i:int = 0; i < list.length; ) {
				if (list[i]==value) {
					delete list.splice(i, 1);
					break;
				}
			}
		}
		public function removeElementByName(name:*):void { 
			for (var i:int = 0; i < list.length; ) {
				if (list[i].name==name) {
					delete list.splice(i, 1);
					break;
				}
			}
		}
		public function removeElementById(id:*):void { 
			for (var i:int = 0; i < list.length; ) {
				if (list[i].id==id) {
					delete list.splice(i, 1);
					break;
				}
			}
		}		
		public function findElementById(id:*):* {
			for (var i:int = 0; i < list.length; ) {
				if (list[i].id==id) {
					return list[i];
				}
			}
			return null;
		}
		public function findElementByName(name:*):* {
			for (var i:int = 0; i < list.length; ) {
				if (list[i].name==name) {
					return list[i];
				}
			}
			return null;
		}		
		///////////////////////////////////////////////
		//
		//
		//					START SORT!
		//
		//
		///////////////////////////////////////////////
		public function setStage(value:*):void {
			stage = value;
		}
		public function onSortX():void {
			if(stage!=null){
				var children:uint = stage.numChildren;
				if(children > 1){
					var index:int = 0;
					list.sortOn("x", Array.NUMERIC);
					for each(var obj:* in list){
						stage.setChildIndex(obj, index++);
					}
				}
			}else {
				trace("stage null, pls check again!");
			}
		}
		public function onSortY():void {
			if(stage!=null){
				var children:uint = stage.numChildren;
				if(children > 1){
					var index:int = 0;
					list.sortOn("y", Array.NUMERIC);
					for each(var obj:* in list){
						stage.setChildIndex(obj, index++);
					}
				}
			}else {
				trace("stage null, pls check again!");
			}
		}
		public function onSortZ():void {
			if(stage!=null){
				var children:uint = stage.numChildren;
				if(children > 1){
					var index:int = 0;
					list.sortOn("z", Array.NUMERIC | Array.DESCENDING);
					for each(var obj:* in list){
						stage.setChildIndex(obj, index++);
					}
				}
			}else {
				trace("stage null, pls check again!");
			}
		}
		
	}
} 
class SingletonListSort{}